package com.bgs.utils;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import java.util.Random;

@Component
public class EmailUtils {
    private String serviceEmail;
    private Logger log= LogManager.getLogger();
    public void sendEmail(String getemail,String msg) throws EmailException {
        serviceEmail = "lc99616@163.com";
        StringBuffer code=getCode();
        try {
            //创建一个HtmlEmail实例对象
            HtmlEmail email = new HtmlEmail();
            //邮箱的SMTP服务器，一般123邮箱的是smtp.123.com , qq邮箱为smtp.qq.com
            email.setHostName("smtp.163.com");
            //设置发送的字符类型
            email.setCharset("utf-8");
            //设置收件人
            email.addTo(getemail);
            //发送人的邮箱为自己的，用户名可以随便填
            email.setFrom(serviceEmail, "审批提交提醒");
            //设置发送人到的邮箱和用户名和授权码(授权码是自己设置的)
            email.setAuthentication(serviceEmail, "YDAETESMUSOSAADY");
            //设置发送主题
            email.setSubject("此邮件无需回复");
            //设置发送内容
            email.setMsg(msg);
            //进行发送00
            email.send();
        } catch (Exception e) {
        }
    }

    public StringBuffer getCode(){
        StringBuffer buffer=new StringBuffer();
        Random random=new Random();
        for(int i=0;i<6;i++){
            buffer.append(random.nextInt(10)+"");
        }
        return buffer;
    }
    public String getServiceEmail() {
        return serviceEmail;
    }

    public void setServiceEmail(String serviceEmail) {
        this.serviceEmail = serviceEmail;
    }
}
